<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
include APPLICATION_PATH . "/models/User.php";
include APPLICATION_PATH."/models/Admin.php";
include APPLICATION_PATH."/models/Agent.php";

class LoginController extends Zend_Controller_Action {

    public function init() {
        $this->modelMapper = new Model_UserMapper();
        $this->model = new Model_User();
        $this->KiemTramodel= new Model_KiemTra();
        $this->Adminmodel=new Model_Admin();
        $this->AdminmodelMapper=new Model_AdminMapper();
        $bootstrap = $this->getInvokeArg("bootstrap");
        $this->aConfig = $bootstrap->getOptions();
        $user = GlobalLib::getUserLogin();
        $this->userid = $user['id'];
        $this->Agentmodel= new Model_Agent();
        $this->AgentmodelMapper= new Model_AgentMapper();
    }

    public function loginAction() {
        $this->_helper->layout()->disableLayout();
        if ($this->_request->isPost()) {
            $username = $this->_getParam("username", "");
            $password = $this->_getParam("password", "");
            $remenber = $this->_getParam("remember", 0);
            
            $errorCode = '';
            if (empty($username)) {
                $errorCode = 2;
            }
            if (empty($password)) {
                $errorCode = 3;
            }
            if(!$this->KiemTramodel->KiemTra($username,$password)){
                $errorCode = 4;
            } else {
                if ($this->KiemTramodel->KiemTra($username,$password)==1) {
                    $this->modelMapper->authenUser($username, $password, $remenber);
                    $url = $this->aConfig["site"]["url"];
                    echo '[{"code":"1","url":"' . $url . 'index/index"}]';
                    exit;
                } else if($this->KiemTramodel->KiemTra($username,$password)==2){
                    $this->AdminmodelMapper->authen($username, $password, $remenber);
                    $url = $this->aConfig["site"]["url"];
                    echo '[{"code":"1","url":"' . $url . 'admin/statistic/index"}]';
                    exit;
                } else if($this->KiemTramodel->KiemTra($username,$password)==3){
                    $this->AdminmodelMapper->authen($username, $password, $remenber);
                      $url = $this->aConfig["site"]["url"];
                    echo '[{"code":"1","url":"' . $url . 'kinhdoanh/index/index"}]';
                    exit;
                }
            }
            if (!empty($errorCode)) {
                echo '[{"code":"' . $errorCode . '"}]';
                exit;
            }
           
        }
    }

    public function logoutAction() {
        $redirectUrl = $this->aConfig["site"]["url"] . "default/login/login";
        GlobalLib::userLogout();
        $this->_redirect($redirectUrl);
    }

    //ADMIN_DOIMATKHAU
    public function editAction() {
        $id = $this->_getParam("id", "");
        $redirectUrl = $this->aConfig["site"]["url"] . "default/index/index";
        if (empty($id)) {
            $this->_redirect($redirectUrl);
        }
        if ($this->userid != $id) {
            $this->_redirect($redirectUrl);
        }
        $this->modelMapper->find($id, $this->model);
        $getId = $this->model->getId();
        $this->AgentmodelMapper->findbyid($getId,$this->Agentmodel);  
        $agentid=$this->Agentmodel->getId();
        if(empty($getId)) {
            $this->_redirect($redirectUrl);
        }
        if ($this->getRequest()->isPost()) {
            if (isset($_POST["id"])) {
                $this->model->setId($_POST["id"]);
            }
            if (strlen($_POST["username"]) > 0) {
                $this->model->setUsername($_POST["username"]);
            }
            
            if (strlen($_POST["first_name"]) > 0) {
                $this->model->setFirst_Name($_POST["first_name"]);
            }
            if (strlen($_POST["last_name"]) > 0) {
                $this->model->setLast_Name($_POST["last_name"]);
            }
            if (isset($_POST["birthday"])) {
                $this->model->setBirthday(GlobalLib::toMysqlDateString($_POST["birthday"]));
            }
            if (isset($_POST["order"])) {
                $this->model->setOrder($_POST["order"]);
            }
            $this->model->setCreated(date("Y/m/d H:i:s"));
            $this->model->setModified(date("Y/m/d H:i:s"));
            $this->modelMapper->save($this->model);
            if($agentid !=NULL){
                $this->Agentmodel->setId($agentid);
                if($_POST["agent_name"]){
                    $this->Agentmodel->setName($_POST["agent_name"]);
                }
                if($_POST["agent_address"]){
                    $this->Agentmodel->setAddress($_POST["agent_address"]);
                }
                if($_POST["legal_capital"]){
                    $this->Agentmodel->setLegal_Capital($_POST["legal_capital"]);
                }
                if($_POST["experience"])
                {
                    $this->Agentmodel->setExperience($_POST["experience"]);
                }
                if($_POST["representavie"]){
                    $this->Agentmodel->setRepresentavie($_POST["representavie"]);
                }
                if($_POST["birthday"]){
                    $this->Agentmodel->setBirthday(GlobalLib::toMysqlDateString($_POST["birthday"]));
                } 
                if (strlen($_POST["phone"]) > 0) {
                    $this->Agentmodel->setPhone($_POST["phone"]);
                }
                if($_POST["ability_sell"]){
                    $this->Agentmodel->setAbility_Sell($_POST["ability_sell"]);
                }
                if($_POST["tax_code"]){
                    $this->Agentmodel->setTax_Code($_POST["tax_code"]);
                }
                if($_POST["person"]){
                    $this->Agentmodel->setPerson($_POST["person"]);
                }
                if($_POST["identity_card"]){
                    $this->Agentmodel->setIdentity_Card($_POST["identity_card"]);
                }
                if($_POST["ability_archive"]){
                    $this->Agentmodel->setAbility_Archive($_POST["ability_archive"]);
                }
                $this->AgentmodelMapper->save($this->Agentmodel);
            }
            $this->_redirect($redirectUrl);
        }
        $this->view->agentitem=$this->Agentmodel;
        $this->view->item = $this->model;
    }

    public function changepasswordAction() {
        $id = $this->_getParam("id", "");
        $oldpassword = $this->_getParam("oldpassword", "");
        $password = $this->_getParam("password", "");
        $this->modelMapper->find($id, $this->model);
        $redirectUrl = $this->aConfig["site"]["url"] . '/default/index/index';
        if ($this->getRequest()->isPost()) {
            if ($this->model->getPassword() == md5($oldpassword)) {
                $this->model->setPassword(md5($password));
                $this->modelMapper->save($this->model);
            }
            echo '[{"html":\'' . $redirectUrl . '\'}]';
            exit();
        }
    }

}
